package cn.edu.scau.cmi.dengshixin.adapter;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class Controller {
	
	@FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableColumn<Student, Integer> idCol;

    @FXML
    private TableColumn<Student, String> nameCol;

    @FXML
    private TextField nameInputField;

    @FXML
    private Button addButton;

    @FXML
    private TextField idInputField;

    @FXML
    private TableView<Student> table;
    
    static ArrayList<Student> studentList;
    static ObservableList<Student> data;
    @FXML
    void getClicked(ActionEvent event) {
    	if(studentList == null) {
    		studentList = new ArrayList<Student>();
    		//data = FXCollections.observableArrayList();
    	}
    	data = FXCollections.observableArrayList();
    	idCol.setCellValueFactory(new PropertyValueFactory<Student, Integer>("id"));
    	nameCol.setCellValueFactory(new PropertyValueFactory<Student, String>("name"));
    	
    	String idInput, nameInput;
    	idInput = idInputField.getText();
    	nameInput = nameInputField.getText();
    	if(idInput.equals("") || nameInput.equals("")) return;
    	idInputField.clear();
    	nameInputField.clear();
    	
    	Integer id = new Integer(idInput);
    	String name = nameInput;
    	Student student = new Student(id, name);
    	studentList.add(student);
    	
    	AdapterOfClass sort = new AdapterOfClass();
    	// AdapterOfObject sort  = new AdapterOfObject();	对象适配器
    	sort.sortStudent(studentList);
    	for(Student s : studentList) {
    		data.add(s);
    	}
    	table.setItems(data);
    }

    @FXML
    void initialize() {
        assert idCol != null : "fx:id=\"idCol\" was not injected: check your FXML file 'AdapterFX.fxml'.";
        assert nameCol != null : "fx:id=\"nameCol\" was not injected: check your FXML file 'AdapterFX.fxml'.";
        assert nameInputField != null : "fx:id=\"nameInputField\" was not injected: check your FXML file 'AdapterFX.fxml'.";
        assert addButton != null : "fx:id=\"addButton\" was not injected: check your FXML file 'AdapterFX.fxml'.";
        assert idInputField != null : "fx:id=\"idInputField\" was not injected: check your FXML file 'AdapterFX.fxml'.";
        assert table != null : "fx:id=\"table\" was not injected: check your FXML file 'AdapterFX.fxml'.";

    }
    
}
